<?php
/**
 * 点大商城（www.diandashop.com） - 微信公众号小程序商城系统!
 * Copyright © 2020 山东点大网络科技有限公司 保留所有权利
 * =========================================================
 * 版本：V2
 * 授权主体：shop.guanjunbang.cn
 * 授权域名：shop.guanjunbang.cn
 * 授权码：vdcajlVLIVAgVdkfJvgY
 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

//买单
namespace app\controller;
use think\facade\Db;
class ApiMaidan extends ApiCommon
{
	public function initialize(){
		parent::initialize();
		$this->checklogin();
	}
	//买单收款
	public function maidan(){
		if(request()->isPost()){
			$post = input('post.');
			$money = floatval($post['money']);
			if($money <=0){
				return $this->json(['status'=>0,'msg'=>'支付金额必须大于0']);
			}
			$paymoney = $money;
			//会员折扣
			$disprice = 0;
			$userlevel = Db::name('member_level')->where('aid',aid)->where('id',$this->member['levelid'])->find();
			if($userlevel && $userlevel['discount']>0 && $userlevel['discount']<10){
				$disprice = $paymoney * (1 - $userlevel['discount'] * 0.1);
			}
			$paymoney = $paymoney - $disprice;
			//优惠券
			if($post['couponrid'] > 0){
				$couponrid = $post['couponrid'];
				$couponrecord = Db::name('coupon_record')->where('aid',aid)->where('mid',mid)->where('id',$couponrid)->find();
				if(!$couponrecord){
					return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'不存在']);
				}elseif($couponrecord['status']!=0){
					return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'已使用过了']);
				}elseif($couponrecord['starttime'] > time()){
					return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'尚未开始使用']);	
				}elseif($couponrecord['endtime'] < time()){
					return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'已过期']);	
				}elseif($couponrecord['minprice'] > $money){
					return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'不符合条件']);
				}elseif($couponrecord['type']!=1){
					return $this->json(['status'=>0,'msg'=>'该'.t('优惠券').'不符合条件']);	
				}
				$couponmoney = $couponrecord['money'];
				if($couponmoney > $money) $couponmoney = $money;
			}else{
				$couponmoney = 0;
			}
			$paymoney = $paymoney - $couponmoney;
			if($paymoney < 0) $paymoney = 0;
			//积分抵扣
			if($post['usescore']==1){
				$adminset = Db::name('admin_set')->where('aid',aid)->find();
				$score2money = $adminset['score2money'];
				$scoredkmaxpercent = $adminset['scoredkmaxpercent'];
				$scoredk = $this->member['score'] * $score2money;
				if($scoredk > $paymoney) $scoredk = $paymoney;
				if($scoredkmaxpercent >= 0 && $scoredkmaxpercent < 100 && $scoredk > 0 && $scoredk > $paymoney * $scoredkmaxpercent * 0.01){
					$scoredk = $paymoney * $scoredkmaxpercent * 0.01;
				}
				$paymoney = $paymoney - $scoredk;
				$paymoney = round($paymoney*100)/100;
				if($paymoney < 0) $paymoney = 0;
				if($scoredk > 0){
					$decscore = $scoredk / $score2money;
				}else{
					$decscore = 0;
				}
			}else{
				$scoredk = 0;
				$decscore = 0;
			}
			$bid = input('param.bid') ? input('param.bid') : 0;
			//$mendian = Db::name('mendian')->where('id',$post['mdid'])->find();
			if($bid > 0){
				$bname = Db::name('business')->where('id',$bid)->value('name');
			}else{
				$bname = Db::name('admin_set')->where('id',aid)->value('name');
			}
			//创建订单
			$order = [];
			$order['ordernum'] = date('ymdHis').aid.rand(1000,9999);
			$order['aid'] = aid;
			$order['bid'] = $bid;
			$order['mid'] = mid;
			$order['title'] = '付款给'.$bname;
			$order['money'] = $money;
			$order['paymoney'] = $paymoney;
			$order['disprice'] = $disprice;
			$order['scoredk'] = $scoredk;
			$order['decscore'] = $decscore;
			$order['couponrid'] = $couponrid;
			$order['couponmoney'] = $couponmoney; //优惠券抵扣
			$order['createtime'] = time();
			$order['platform'] = platform;
			$order['status'] = 0;
			$order['mdid'] = $post['mdid'];
			$orderid = Db::name('maidan_order')->insertGetId($order);

			$payorderid = \app\model\Payorder::createorder(aid,$order['bid'],$order['mid'],'maidan',$orderid,$order['ordernum'],$order['title'],$order['paymoney'],$order['decscore']);

			return $this->json(['status'=>1,'payorderid'=>$payorderid]);
		}
		$bid = input('param.bid') ? input('param.bid') : 0;
		$adminset = Db::name('admin_set')->where('aid',aid)->find();
		$userlevel = Db::name('member_level')->where('aid',aid)->where('id',$this->member['levelid'])->find();
		$userinfo = [];
		$userinfo['discount'] = $userlevel['discount'];
		$userinfo['score'] = $this->member['score'];
		$userinfo['score2money'] = $adminset['score2money'];
		$userinfo['dkmoney'] = round($userinfo['score'] * $userinfo['score2money'],2);
		$userinfo['scoredkmaxpercent'] = $adminset['scoredkmaxpercent'];
		$userinfo['money'] = $this->member['money'];
		if($this->member['paypwd']==''){
			$userinfo['haspwd'] = 0;
		}else{
			$userinfo['haspwd'] = 1;
		}
		if($bid)
		$bname = Db::name('business')->where('aid',aid)->where('id', $bid)->value('name');
		$couponList = Db::name('coupon_record')
			->where('aid',aid)->where('mid',mid)->where('bid',$bid)->where('type',1)->where('status',0)->where('starttime','<=',time())->where('endtime','>',time())
			->order('id desc')->select()->toArray();
		if(!$couponList) $couponList = [];
		$newcouponlist = [];
		foreach($couponList as $k=>$v){
			//$couponList[$k]['starttime'] = date('m-d H:i',$v['starttime']);
			//$couponList[$k]['endtime'] = date('m-d H:i',$v['endtime']);
            $v['bname'] = $bname;
			$couponinfo = Db::name('coupon')->where('aid',aid)->where('id',$v['couponid'])->find();
			if($couponinfo['fwtype']!==0){//全部可用
				continue;
			}
            if($couponinfo['isgive']==2){//仅赠送
                continue;
            }
			$newcouponlist[] = $v;
		}
		$couponList = $newcouponlist;
		//门店
		$mdlist = Db::name('mendian')->where('aid',aid)->where('status',1)->where('bid',$bid)->order('id')->select()->toArray();
		if(!$mdlist) $mdlist = [];
		$set = Db::name('admin_set')->where('aid',aid)->find();
		if($bid > 0){
			$set['name'] = Db::name('business')->where('id',$bid)->value('name');
			$set['logo'] = Db::name('business')->where('id',$bid)->value('logo');
		}
		$rdata = [];
		$rdata['userinfo'] = $userinfo;
		$rdata['couponList'] = $couponList;
		$rdata['wxpayst'] = $set['wxpay'];
		$rdata['alipay'] = $set['alipay'];
		$rdata['moneypay'] = $set['moneypay'];
		$rdata['name'] = $set['name'];
		$rdata['logo'] = $set['logo'];
		$rdata['mdlist'] = $mdlist;
		return $this->json($rdata);
	}

	public function maidanlog(){
		$pagenum = input('post.pagenum');
        $st = input('post.st');
		if(!$pagenum) $pagenum = 1;
		$pernum = 20;
		$where = [];
		$where[] = ['aid','=',aid];
		$where[] = ['mid','=',mid];
		$where[] = ['status','=',1];
		if(input('param.keyword')) $where[] = ['ordernum','like','%'.input('param.keyword').'%'];
		$datalist = Db::name('maidan_order')->field('*,from_unixtime(paytime)paytime')->where($where)->page($pagenum,$pernum)->order('id desc')->select()->toArray();
		if(!$datalist) $datalist = [];
		if(request()->isPost()){
			return $this->json(['status'=>1,'data'=>$datalist]);
		}
		$count = Db::name('maidan_order')->where($where)->count();
		$rdata = [];
		$rdata['count'] = $count;
		$rdata['datalist'] = $datalist;
		$rdata['pernum'] = $pernum;
		$rdata['st'] = $st;
		return $this->json($rdata);
	}
	public function maidandetail(){
		$id = input('param.id/d');
		$detail = Db::name('maidan_order')->where('aid',aid)->where('mid',mid)->where('id',$id)->find();
		$detail['paytime'] = date('Y-m-d H:i:s',$detail['paytime']);
		if($detail['couponrid']){
			$couponrecord = Db::name('coupon_record')->where('aid',aid)->where('mid',mid)->where('id',$detail['couponrid'])->find();
		}else{
			$couponrecord = false;
		}
		if($detail['mdid']){
			$mendian = Db::name('mendian')->field('id,name')->where('aid',aid)->where('id',$detail['mdid'])->find();
		}else{
			$mendian = false;
		}
		$rdata = [];
		$rdata['detail'] = $detail;
		$rdata['couponrecord'] = $couponrecord;
		$rdata['mendian'] = $mendian;
		return $this->json($rdata);
	}
}